package com.sgcc.fj.pmos.mobileapp.presenter;


import android.text.TextUtils;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.fj.pmos.common.base.BasePresenter;
import com.sgcc.fj.pmos.common.bean.FaceGetCodeBean;
import com.sgcc.fj.pmos.common.http.HttpInterConst;
import com.sgcc.fj.pmos.common.http.HttpRequestUtils;
import com.sgcc.fj.pmos.common.http.MvpNetCallback;
import com.sgcc.fj.pmos.mobileapp.contract.VideoAttestationContract;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.MultipartBody;


public class VideoAttestationPresenter extends BasePresenter<VideoAttestationContract.View>
        implements VideoAttestationContract.Presenter {


    @Override
    public void getCode(String name, String idCode) {


        Map<String, Object> params = new HashMap<>();
        params.put("doucument_number", name);
        params.put("creditcode", idCode);

        HttpRequestUtils.getInstance().request(params, HttpInterConst.FACE_GET_CODE,
                new MvpNetCallback<FaceGetCodeBean>(getView(), true) {
                    @Override
                    public void onResponse(FaceGetCodeBean response, int id) {
                        super.onResponse(response, id);
                        if (null != response) {
                            if (null != response.getData()) {
                                getView().getCodeSuccess(response.getDataBean());
                            } else {
                                if (!TextUtils.isEmpty(response.getMessage()))
                                    ToastUtils.showShort(response.getMessage());
                                getView().onFailed(response.getStatus());
                            }
                        }

                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                    }
                });


    }

    @Override
    public void checkResult(String sessionId, String realName, String id, String videoBase64) {


        OkHttpUtils.post().tag(ActivityUtils.getTopActivity())
                .mediaType(MultipartBody.FORM)
                .addParams("type", "faceliveness_verify_zhuce")
                .addParams("apiType", "faceliveness")
                .addParams("session_id", sessionId)
                .addParams("realName", realName)
                .addParams("ID", id)
                .addParams("video_base64", videoBase64)
                .url(HttpInterConst.CHECK_RESULT).build()
                .execute(new MvpNetCallback<FaceGetCodeBean>(getView()) {
                    @Override
                    public void onResponse(FaceGetCodeBean response, int id) {
                        super.onResponse(response, id);
                        if (null != response && response.getStatus() == 1) {
                            getView().checkResultSuccess(response.getData());
                        } else {
                            getView().onFailed(3);
                            ToastUtils.showShort(response.getMessage());
                        }


                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                    }
                });


    }
}
